#include"Sort.h"





void _MergeSort(int* a,int* temp, int left, int right)
{
	if (left >= right)
		return;
	int mid = (left + right) / 2;
	_MergeSort(a,temp, left, mid);
	_MergeSort(a,temp, mid + 1, right);
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int n = begin1, i = begin1;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			temp[n++] = a[begin1++];
		}
		else
		{
			temp[n++] = a[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		temp[n++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		temp[n++] = a[begin2++];
	}
	memcpy(a + i, temp + i, sizeof(int) * (end2-i+1));
}



void MergeSort(int* a, int n)
{
	int* temp = (int*)malloc(sizeof(int) * n);
	_MergeSort(a, temp, 0, n - 1);
}

void MergeSortNonR(int* a, int n)
{
	int* temp = (int*)malloc(sizeof(int) * n);
	if (temp == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i = i + 2 * gap)
		{

			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			int num = begin1;
			if (begin2 >= n)
			{
				break;
			}
			if (end2 >= n)
			{
				end2 = n - 1;
			}
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					temp[num++] = a[begin1++];
				}
				else
				{
					temp[num++] = a[begin2++];
				}
			}
			while (begin1 <= end1)
			{
				temp[num++] = a[begin1++];
			}
			while (begin2 <= end2)
			{
				temp[num++] = a[begin2++];
			}
			memcpy(a + i, temp + i,sizeof(int) * (end2 - i + 1));
		}
		gap *= 2;
	}
}

void Swap(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}

int  PartSort1(int* a, int begin, int end)
{
	int keyi = begin;
	while (begin < end)
	{
		while (a[end] >= a[keyi]&&begin<end)
		{
			end--;
		}
		while (a[begin] <= a[keyi]&&begin<end)
		{
			begin++;
		}
		Swap(&a[begin], &a[end]);
	}
	Swap(&a[end], &a[keyi]);
	return end;
}

int PartSort2(int* a, int begin, int end)
{
	int key = a[begin];
	while (begin < end)
	{
		while (a[end] >= key && begin < end)
		{
			end--;
		}
		a[begin] = a[end];
		while (a[begin] <= key && begin < end)
		{
			begin++;
		}
		a[end] = a[begin];
	}
	Swap(&a[end], &key);
	return end;
}

int PartSort3(int* a, int begin, int end)
{
	int keyi = begin;
	int prev = begin;
	int cur = prev + 1;
	while (cur <= end)
	{
		if (a[cur] <= a[keyi] && ++prev != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[prev], &a[keyi]);
	return prev;
}

void QuickSort(int* a, int left,int right)
{
	/*if (left >= right)
		return;
	int keyi = PartSort1(a, left, right);
	QuickSort(a, left, keyi - 1);
	QuickSort(a, keyi + 1, right);*/

	//if (left >= right)
	//	return;
	//int keyi = PartSort2(a, left, right);
	//QuickSort(a, left, keyi - 1);
	//QuickSort(a, keyi + 1, right);

	if (left >= right)
		return;
	int keyi = PartSort3(a, left, right);
	QuickSort(a, left, keyi - 1);
	QuickSort(a, keyi + 1, right);
}

void QuickSortNonR(int* a, int left, int right)
{
	Stack s;
	StackInit(&s);
	StackPush(&s, right);
	StackPush(&s, left);
	while (!StackEmpty(&s))
	{
		int begin = StackTop(&s);
		StackPop(&s);
		int end = StackTop(&s);
		StackPop(&s);
		int keyi = PartSort3(a, begin, end);
		if (keyi + 1 < end)
		{
			StackPush(&s, end);
			StackPush(&s, keyi + 1);
		}
		if (keyi - 1 > begin)
		{
			StackPush(&s, keyi - 1);
			StackPush(&s, begin);
		}
	}
}